#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n, m;
    cin >> n >> m;
    int ans  =  0;
    vector<string> grid(n);
    for(int i = 0; i < n; i++) cin >> grid[i];
    int len = min(n, m) / 2;
    for(int i = 0; i < len; i++){
        int w = i, a = i;
        int s = n - i - 1, d = m - i - 1;
        string now;
        for(int j = a; j <= d; j++) now += grid[w][j];
        for(int j = w + 1; j <= s; j++) now += grid[j][d];
        if(w != s){
            for(int j = d - 1; j >= a; j--) now += grid[s][j];
        }
        if(a != d){
            for(int j = s - 1; j > w; j--) now += grid[j][a];
        }
        // cout << now << endl;
        now += now.substr(0, 3);
        for(int i = 0; i <= now.size() - 4; i++){
            if(now.substr(i, 4) == "1543")
                ans++;
        }
    }
    cout << ans << endl;
}

int main(){
    int t;
    cin >> t;

    while(t--) solve();

    return 0;
}